package kubayaxi.video.service.base.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @Author linyx
 * @Description
 * @Date 2022/8/30 16:52
 */
@Configuration
@Slf4j
public class RestTemplateConfig {
	/**
	 * restTemplate配置
	 */
	@Bean
	@Scope(value = "prototype")
	public RestTemplate restTemplate() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(5000); // 设置读取时的过期时间为5000毫秒
		factory.setConnectTimeout(5000); // 设置连接超时时间为5000毫秒
		factory.setBufferRequestBody(false); // 使用内部缓存缓存请求的body体，默认为true，如果post或put的数据量比较大，建议关闭
		RestTemplate restTemplate = new RestTemplate(factory);
        //restTemplate.setRequestFactory(clientHttpRequestFactory()); // 设置请求的工厂
//        restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); // 设置请求错误的处理
		return restTemplate;
	}

//	@Bean
//	public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
//		// 代理https
//		try {
//			TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
//			SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
//			SSLConnectionSocketFactory connectionSocketFactory =
//					new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
//
//			HttpClientBuilder httpClientBuilder = HttpClients.custom();
//			httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
//			// 代理url, port
//			httpClientBuilder.setProxy(new HttpHost("8.140.154.128", 3128, "http"));
//			CloseableHttpClient httpClient = httpClientBuilder.build();
//			HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
//			factory.setHttpClient(httpClient);
//			factory.setConnectTimeout(10 * 1000);
//			factory.setReadTimeout(30 * 1000);
//			return factory;
//		} catch (Exception e) {
//			log.error("创建HttpsRestTemplate失败", e);
//			throw new RuntimeException("创建HttpsRestTemplate失败", e);
//		}
//	}


		// 失败重试
//		try {
//			HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//				public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
//					return true;
//				}
//			}).build();
//			httpClientBuilder.setSSLContext(sslContext);
//			HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
//			SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
//					hostnameVerifier);
//			Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
//					.register("http", PlainConnectionSocketFactory.getSocketFactory())
//					.register("https", sslConnectionSocketFactory).build();// 注册http和https请求
//			// 开始设置连接池
//			PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(
//					socketFactoryRegistry);
//			httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
//			httpClientBuilder.setServiceUnavailableRetryStrategy(new MyDefaultServiceUnavailableRetryStrategy());
//			httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(1, true)); // 重试次数
//			HttpClient httpClient = httpClientBuilder.build();
//			return new HttpComponentsClientHttpRequestFactory(httpClient);
//		} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
//			System.out.println("初始化HTTP连接池出错");
//		}
//		return null;
//	}
}
